$(document).ready(function() {
	/* Search */
	$('.button-search').bind('click', function() {
		url = $('base').attr('href') + 'search';
				 
		var keyword = $('input[name=\'keyword\']').attr('value');
		
		if (keyword) {
			url += '/' + encodeURIComponent(keyword);
		}
		
		location = url + '.html';
	});
	
	$('#menu input[name=\'keyword\']').bind('keydown', function(e) {
		if (e.keyCode == 13) {
			url = $('base').attr('href') + 'search';
			 
			var keyword = $('input[name=\'keyword\']').attr('value');
			
			if (keyword) {
				url += '/' + encodeURIComponent(keyword);
			}
			
			location = url + '.html';
		}
	});
	
	/* Ajax Cart */
	$('#cart > .heading').live('mouseover', function() {
		$('#cart').addClass('active');
		
		$('#cart').load('index.php?route=module/cart #cart > *');
		
		$('#cart').live('mouseleave', function() {
			$(this).removeClass('active');
		});
	});
		
	$('.success img, .warning img, .attention img, .information img').live('click', function() {
		$(this).parent().fadeOut('slow', function() {
			$(this).remove();
		});
	});	
	
	$('#currency_down').bind('mouseover', function() {
		$('#currency_dl').show();
	});
		
	$('#currency_dl').bind('mouseover', function() {
		$('#currency_dl').show();
	});	
	$('#currency_dl').bind('mouseout', function() {
		$('#currency_dl').hide();
	});
	
});

function getURLVar(urlVarName) {
	var urlHalves = String(document.location).toLowerCase().split('?');
	var urlVarValue = '';
	
	if (urlHalves[1]) {
		var urlVars = urlHalves[1].split('&');

		for (var i = 0; i <= (urlVars.length); i++) {
			if (urlVars[i]) {
				var urlVarPair = urlVars[i].split('=');
				
				if (urlVarPair[0] && urlVarPair[0] == urlVarName.toLowerCase()) {
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	
	return urlVarValue;
} 

function addToCart(product_id, quantity) {
	quantity = typeof(quantity) != 'undefined' ? quantity : 1;

	$.ajax({
		url: 'index.php?route=checkout/cart/add',
		type: 'post',
		data: 'product_id=' + product_id + '&quantity=' + quantity,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information, .error').remove();
			
			if (json['redirect']) {
				location = json['redirect'];
			}
			
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#cart-total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}
function addToWishList(product_id) {
	$.ajax({
		url: 'index.php?route=account/wishlist/add',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#wishlist-total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow');
			}	
		}
	});
}

function addToCompare(product_id) { 
	$.ajax({
		url: 'index.php?route=product/compare/add',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#compare-total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}
/*autoComplete Plugin*/
(function($){
	/* Plugin */
	$.fn.autoComplete=function(o){
	    var handler=(function(){
	        var handler=function(e,o){ return new handler.prototype.init(e,o); };
	        handler.prototype={
	            e:null, o:null, timer:null, show:0, $input:null, $popup:null,
	            init:function(e,o){
	                this.e=e;
	                this.o=o;
	                this.$input=$("input",this.e);
	                this.$popup=$("ul",this.e);
	                this.initEvent();
	            },
	            showResult:function(data, a_url){
	            	this.$popup.html('');
	            	this.$popup.hide();
	            	if(data != null && data != "-999"){
	            		var ks = data.split(",");
                    	for(var i = 0; i < ks.length; i++){
                    		k = ks[i].split("|");
    	                	this.$popup.append('<li><a href="' + a_url + k[1] + '.html">' + k[1] + '</a></li>');
    	                }
    	                this.$popup.show(); 
                    }	            	
	            },
	            fetch:function(ajax,search,a_url){
	            	if(search != null){
	            		search = $.trim(search);
                        if(search != ''){
                        	var that=this;
        	                $.ajax({
        	                    url: ajax.url + search,
        	                    dataType: ajax.dataType,
        	                    async: ajax.async,
        	                    error: function(data,es,et){that.$popup.html('');},
        	                    success: function(data){that.showResult(data, a_url); }
        	                });
                        }else{
                        	this.$popup.html('');
                        }
                    }else{
                    	this.$popup.html('');
                    }
	            },
	            initEvent:function(){
	                var that=this;
	                this.$input.focus(function(){
	                	var value = this.value, self = this, t_num = 1;
	                	that.timer=setInterval(function(){
	                        if(value != self.value){
	                            $("#testdiv").text(value + "--" + self.value + "--" + t_num);
	                            t_num = t_num + 1;
	                            value = self.value;
	                            that.fetch(that.o.ajax,value,that.o.a_url);
	                        }
	                    },1000);
	                }).blur(function(){
	                    clearInterval(that.timer);
	                }).keydown(function(event){
	                    if(event.keyCode==40){
	                        that.$popup.blur().find("li a").get(0).focus();
	                    }
	                });
	                $(this.e).hover(function(){ that.show=1; },function(){ that.show=0; });
	                $(document).click(function(){ if(that.show==0){ that.$popup.hide(); } });
	            }
	        };
	        handler.prototype.init.prototype=handler.prototype;/* JQuery style */
	        return handler;
	    })();
	 
	    return this.each(function(){ handler(this,o); });
	};
	/* Invoke */
	$(document).ready(function(){
	    $(".autoComplete").autoComplete({
	    	ajax:{ url:'index.php?route=search/search/findKeyword&keyword=', dataType:'text', async:true },
	    	a_url:'search/'
	    });
	});
	/* Conclude */
})(jQuery);